package com.captain.special.二叉树;

import com.captain.leetcode.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * Des:
 *
 * @author XL
 * @Date 2022/4/24 20:24
 */
public class 左叶子之和404 {

    public int sumOfLeftLeaves(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        recur(root, res, false);
        int sum = 0;
        for (Integer re : res) {
            sum += re;
        }
        return sum;
    }

    public void recur(TreeNode node, List<Integer> res, boolean b) {
        if (node.left == null && node.right == null && b) {
            res.add(node.val);
        }
        if (node.left != null) {
            recur(node.left,res,true);
        }
        if (node.right != null) {
            recur(node.right, res , false);
        }
    }
}
